package com.example.physicalexaminationsystem.service;

import com.example.physicalexaminationsystem.dto.ResponseDto;
import com.example.physicalexaminationsystem.mapper.DepartmentDao;
import com.example.physicalexaminationsystem.mapper.ProjectDao;
import com.example.physicalexaminationsystem.model.DepartmentEntity;
import com.example.physicalexaminationsystem.model.ProjectEntity;
import com.example.physicalexaminationsystem.vo.ProjectVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author shiwenzhuo
 * 
 * @description
 */
@Component
public class ProjectService {
    @Resource
    private ProjectDao projectDao;
    @Resource
    private DepartmentDao departmentDao;

    //获取项目信息
    public List<ProjectVO> getProjectList() {
        List<ProjectEntity> projectEntities = projectDao.selectList(null);
        return projectEntities.stream().map(this::ProjectEntityToProjectVO).collect(Collectors.toList());
    }

    //添加项目信息
    public ResponseDto addProject(Map<String, Object> params) {
        Long departmentId = Long.valueOf(params.get("departmentId").toString());
        DepartmentEntity departmentEntity = departmentDao.selectById(departmentId);
        if (departmentEntity == null) {
            return ResponseDto.fail("科室不存在");
        }
        String name = params.get("name").toString();
        Integer price = Integer.valueOf(params.get("price").toString());
        Double maxLevel = Double.valueOf(params.get("maxLevel").toString());
        Double minLevel = Double.valueOf(params.get("minLevel").toString());
        String info = params.get("info").toString();
        ProjectEntity projectEntity = new ProjectEntity();
        projectEntity.setDepartmentId(departmentId);
        projectEntity.setName(name);
        projectEntity.setPrice(price);
        projectEntity.setInfo(info);
        projectEntity.setMaxLevel(maxLevel);
        projectEntity.setMinLevel(minLevel);
        projectDao.insert(projectEntity);
        return ResponseDto.success();
    }

    //删除项目信息根据id
    public void deleteProjectById(Long id) {
        projectDao.deleteById(id);
    }

    //修改项目信息根据id
    public ResponseDto updateProjectById(Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        Long departmentId = Long.valueOf(params.get("departmentId").toString());
        DepartmentEntity departmentEntity = departmentDao.selectById(departmentId);
        if (departmentEntity == null) {
            return ResponseDto.fail("科室不存在");
        }
        String name = params.get("name").toString();
        Integer price = Integer.valueOf(params.get("price").toString());
        String info = params.get("info").toString();
        ProjectEntity projectEntity = new ProjectEntity();
        projectEntity.setId(id);
        projectEntity.setDepartmentId(departmentId);
        projectEntity.setName(name);
        projectEntity.setPrice(price);
        projectEntity.setInfo(info);
        projectDao.updateById(projectEntity);
        return ResponseDto.success();
    }

    //查看所有项目信息
    public List<ProjectVO> getAllProject() {
        List<ProjectEntity> projectEntities = projectDao.selectList(null);
        List<ProjectVO> projectVOS = new ArrayList<>();
        for (ProjectEntity projectEntity : projectEntities) {
            ProjectVO projectVO = ProjectEntityToProjectVO(projectEntity);
            projectVOS.add(projectVO);
        }
        return projectVOS;
    }

    public ProjectVO ProjectEntityToProjectVO(ProjectEntity projectEntity) {
        ProjectVO projectVO = new ProjectVO();
        projectVO.setId(projectEntity.getId());
        projectVO.setName(projectEntity.getName());
        projectVO.setPrice(projectEntity.getPrice());
        projectVO.setInfo(projectEntity.getInfo());
        projectVO.setDepartmentName(departmentDao.selectById(projectEntity.getDepartmentId()).getName());
        return projectVO;
    }

}
